package com.finger.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finger.commonutils.R;
import com.finger.eduservice.client.VodClient;
import com.finger.eduservice.entity.EduVideo;
import com.finger.eduservice.mapper.EduVideoMapper;
import com.finger.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-06
 */
@Log4j2
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    /**
     * 根据课程id删除小节
     * @param courseId
     */
    @Override
    public void removeVideoByCourseId(String courseId) {
        // 删除小节的wrapper
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();

        // 获取小节视频资源id的wrapper
        QueryWrapper<EduVideo> eduVideoQueryWrapper2 = new QueryWrapper<>();

        eduVideoQueryWrapper2.eq("course_id",courseId);

        eduVideoQueryWrapper2.select("video_source_id");

        // 小节视频资源id对象的集合
        List<EduVideo> eduVideos = baseMapper.selectList(eduVideoQueryWrapper2);

        List<String> ids = new ArrayList<>();

        // 如果eduVideos里边有值
        if(eduVideos.size() !=0){
            for (EduVideo eduVideo : eduVideos) {

                String videoSourceId = eduVideo.getVideoSourceId();

                if(!StringUtils.isEmpty(videoSourceId)){

                    ids.add(videoSourceId);
                }
            }
            // 如果ids不为空
            if(ids.size()!=0){
                vodClient.deleteBatch(ids);
            }
        }

        eduVideoQueryWrapper.eq("course_id",courseId);

        baseMapper.delete(eduVideoQueryWrapper);
    }



}
